/*
	File:		MainMenuState.cpp
	Author:		Arturo Mata, dmonroe
	Date:		11/4/2012
	Copyright:	Full Sail University

	Purpose:	State to handle main menu
*/

#include "../source/MainMenuState.h"

#include <cassert>

#include "../source/IAudio.h"
#include "../source/IGraphics.h"
#include "../source/IInput.h"

#include "../source/Constants.h"
#include "../source/Resources.h"

#include "../source/IGameStateMachine.h"
#include "../source/BitmapFont.h"


//******************************************************************
// SINGLETON

// Instantiate static pointer to null (no instance yet)
/*static*/ CMainMenuState*	CMainMenuState::s_Instance		= NULL;

// Interface singleton accessor
/*static*/ IGameState*		CMainMenuState::GetInstance		( void )
{
	// Allocate singleton on first use
	if( CMainMenuState::s_Instance == NULL )
		CMainMenuState::s_Instance = new CMainMenuState;

	// Return the singleton
	return CMainMenuState::s_Instance;
}

// Interface singleton destructor
/*virtual*/ void			CMainMenuState::DeleteInstance	( void )
{
	// Deallocate singleton
	delete CMainMenuState::s_Instance;
	CMainMenuState::s_Instance = NULL;
}
//******************************************************************



//******************************************************************
// CONSTRUCTOR
CMainMenuState::CMainMenuState( void )
{
	// Set all the data members to 'clean' values
	m_pAudio			= NULL;
	m_pGraphics			= NULL;
	m_pInput			= NULL;	
	
	m_pWorld			= NULL;
	
	m_hTexture			= HTexture();

	m_pFont = NULL;
}
//******************************************************************



//******************************************************************
// DESTRUCTOR
CMainMenuState::~CMainMenuState( void )
{
}
//******************************************************************



//******************************************************************
// ENTER
/*virtual*/ bool CMainMenuState::Enter( void )
{
	// Access the wrappers
	m_pAudio			= IAudio::GetInstance();
	m_pGraphics			= IGraphics::GetInstance();
	m_pInput			= IInput::GetInstance();

	// Load testing file
	m_hTexture = m_pGraphics->LoadTexture( GRAPHICS_PATH( "MainMenuState" ) );
	assert( m_hTexture != INVALID_HANDLE && "CMainMenuState::Enter - could not load MainMenuState image" != 0 );

	if( m_hTexture == INVALID_HANDLE )
		return false;

	m_pFont = new BitmapFont();

	return true;
}
//******************************************************************



//******************************************************************
// EXIT
/*virtual*/ bool CMainMenuState::Exit( void )
{
	// Unload assets
	m_pGraphics->UnloadTexture( m_hTexture );
	m_hTexture = HTexture();

	if (m_pFont != nullptr)
	{
		delete m_pFont;
		m_pFont = nullptr;
	}


	return true;
}
//******************************************************************



//******************************************************************
// INPUT
/*virtual*/ bool CMainMenuState::Input( float fElapsedTime )
{
	(void)fElapsedTime;		// unused for now


	// Exit game?
	if( m_pInput->ButtonPressed( IInput::E_SELECT ) == true )
		return false;
	//E_MAINMENU, E_GAMEPLAY, E_OPTIONS, E_HOWTOPLAY, E_HIGHSCORES, E_CREDITS
	// Switch to gameplay?
	else if( m_pInput->ButtonPressed( IInput::E_START ) == true )
		return IGameStateMachine::GetInstance()->SwitchState( IGameStateMachine::E_GAMEPLAY );
	else if( m_pInput->ButtonPressed( IInput::E_DPAD_UP ) == true )
		return IGameStateMachine::GetInstance()->SwitchState( IGameStateMachine::E_OPTIONS );
	else if (m_pInput->ButtonPressed(IInput::E_DPAD_RIGHT) == true)
		return IGameStateMachine::GetInstance()->SwitchState( IGameStateMachine:: E_HOWTOPLAY);

	return true;
}
//******************************************************************



//******************************************************************
// UPDATE
/*virtual*/ bool CMainMenuState::Update( float fElapsedTime )
{
	(void)fElapsedTime;		// unused for now


	return true;
}
//******************************************************************



//******************************************************************
// RENDER
bool CMainMenuState::Render( float fElapsedTime )
{	
	(void)fElapsedTime;		// unused for now


	// draw stuff
	m_pGraphics->DrawTexture( m_hTexture, 0, 0, 0, 0, 0, SColor(), 2, 2 );

	//m_pFont->Print("TemporaryFont Test", 0, 0, 1.0f, SColor(0,255,0,255));	//something is goofy??

	return true;
}
//******************************************************************
